/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
var Func = {
    createNew: function () {

    }
};
/**
 * 取的bridge对象
 * @param {type} callbackFunc
 * @returns {undefined}
 */
Func.getBridge = function (callbackFunc) {
    if (browser.versions.android)
        callbackFunc(jsb);
    else {
        JavascriptBridge.connectWB(function (bridge) {
            bridge.init(function (message, responseCallback) {
                // alert("init")
                responseCallback(message);
            });
            callbackFunc(bridge);
        });
    }
};
/**
 * 注册APP调用函数-包含bridge
 * @param {type} funcName
 * @param {type} excuteFunc
 * @returns {undefined}
 */
Func.registerHanderBridge = function (funcName, excuteFunc) {
    if (browser.versions.android) {
        jsb.addJavascriptMethod(funcName, function (params) {
            excuteFunc(params, jsb);
        });
    } else {
        JavascriptBridge.connectWB(function (bridge) {
            bridge.registerHandler(funcName, function (params) {
                excuteFunc(params, bridge);
            });
        });
    }
};

/**
 * 注册APP调用函数-包含bridge-带返回值
 * @param {type} funcName
 * @param {type} excuteFunc
 * @returns {undefined}
 */
Func.registerHanderBridgeResp = function (funcName, excuteFunc) {
    if (browser.versions.android) {
        jsb.addJavascriptMethod(funcName, function (params) {
            var back = excuteFunc(params, jsb);
            return back;
        });
    } else {
        JavascriptBridge.connectWB(function (bridge) {
            bridge.registerHandler(funcName, function (params, callbackFunc) {
                var back = excuteFunc(params, bridge);
                callbackFunc(back);
            });
        });
    }
};

/**
 * 注册APP调用函数
 * @param {type} funcName
 * @param {type} excuteFunc
 * @returns {undefined}
 */
Func.registerHander = function (funcName, excuteFunc) {
    if (browser.versions.android) {
        jsb.addJavascriptMethod(funcName, function (params) {
            excuteFunc(params);
        });
    } else {
        JavascriptBridge.connectWB(function (bridge) {
            bridge.registerHandler(funcName, function (params) {
                excuteFunc(params);
            });
        });
    }
};

/**
 * 注册APP调用函数-带返回值
 * @param {type} funcName
 * @param {type} excuteFunc
 * @returns {undefined}
 */
Func.registerHanderResp = function (funcName, excuteFunc) {
    if (browser.versions.android) {
        jsb.addJavascriptMethod(funcName, function (params) {
            var back = excuteFunc(params);
            return back;
        });
    } else {
        JavascriptBridge.connectWB(function (bridge) {
            bridge.registerHandler(funcName, function (params, callbackFunc) {
                var back = excuteFunc(params);
                callbackFunc(back);
            });
        });
    }
};

/**
 * 调用APP端函数
 * @param {type} bridge
 * @param {type} funcName
 * @param {type} params
 * @returns {undefined}
 */
Func.callHander = function (bridge, funcName, params) {
    if (browser.versions.android) {
        bridge.require(funcName, params);
    } else {
        bridge.callHandler(funcName, params);
    }
};
/**
 * 调用APP端函数 并接收返回值
 * @param {type} bridge
 * @param {type} funcName
 * @param {type} params
 * @param {type} callbackFunc
 * @returns {undefined}
 */
Func.callHanderResp = function (bridge, funcName, params, callbackFunc) {
    if (browser.versions.android) {
        bridge.require(funcName, params, function (response) {
            callbackFunc(response);
        });
    } else {
        bridge.callHandler(funcName, params, function (response) {
            callbackFunc(response);
        });
    }
};
